import get_news from './modules/sina/sina_spider.js'
import {get_web_info, getInfoByBook} from './modules/web/web_spider.js'
import MoudleA from './modules/web/5000yan.js'
import MoudleB from './modules/web/howToCook.js'
import get_web_info2 from './modules/web/web_spider2.js'

import express from 'express';
import { addSql,getResult, onDuplicByFiled } from '../../util/operateSql.js';
var router = express.Router();


const baseUrl = 'https://s.weibo.com'
const urls = ['/top/summary?cate=realtimehot',
'/top/summary?cate=socialevent',
'/top/summary?cate=entrank']

let item = baseUrl + urls[0]

/**
 * 
 * @param {Object} type: 新闻类型 
 * @returns 新闻列表
 */
function  getNews(params) {
  return new Promise((resolve, reject) => {
    if (params.type === 'hot') item = baseUrl + urls[0]
    else if (params.type === 'social') item = baseUrl + urls[1]
    else item = baseUrl + urls[2]
    get_news(item, (rows) => {
      if (rows) {
        resolve(rows)
      } else {
        reject(rows)
      }
    })
  });
}

router.get('/crawl/sina', (req,res, next) => {
  const params = req.query
  getNews(params).then((rows) => {
    res.json(rows)
  }).catch((err) => {
    res.json(err)
  });
})


router.get('/web1', (req,res, next) => {
  const params = req.query

  // 手动遍历
  // for (let index = 0; index < 100; index++) {
    params.page = index || null
    get_web_info('http://www.oncer.cn/modules/article/articlefilter.php',params,(row) => {
      if (row.code == 0) {
        row.data.forEach((e,index) => {
          const sql = addSql('spider.story',e)
          getResult(sql).then((result) => {
            
          }).catch((err) => {
            const sql = onDuplicByFiled('spider.story',e)
            getResult(sql).then((result) => {
              
            }).catch((err) => {
              
              
            });
          });
        })
  
      }
      res.json(row)
    })
  // }
})
router.get('/web1/info', (req,res, next) => {
  const params = req.query
    
    getInfoByBook(params,(row) => {
      res.json(row)
    })
  
    
})


router.get('/web2', (req,res, next) => {
  const params = req.query
  get_web_info2('https://www.oklink.com/api/explorer/v1/btc/transactionsNoRestrict?t=1659512959302&limit=20&offset=0',(row) => {
    res.json(row)
  })
})

router.get('/5000yan', (req,res, next) => {
  const params = req.query
  MoudleA('https://zhongcaoyao.5000yan.com/',{},(row) => {
    res.json(row)
  })
})
router.get('/howToCook', (req,res, next) => {
  const params = req.query
  MoudleB('https://cook.aiurs.co/dishes/vegetable_dish/%E6%8B%94%E4%B8%9D%E5%9C%9F%E8%B1%86/%E6%8B%94%E4%B8%9D%E5%9C%9F%E8%B1%86/',{},(row) => {
    res.json(row)
  })
})


// 简单 https://www.voyah.com.cn/service.html 

// 加密算法 (https://www.theone.art/market?type=copyright)

// SM2 + SM4加密 :https://fuwu.nhsa.gov.cn/nationalHallSt/#/search/medical 

// (进阶 cookie加密):IC 交易网----IC电子元器件交易平台  随便点一个热门搜索的 需要登录

// (请求参数加密 爬虫入门)  https://www.oklink.com/zh-cn/btc/tx-list

// 高级进阶需要登录h5页面:https://v.7jft.com/h5/#/pages/homePages/search?value=%E9%A3%9E%E8%88%B9

export {getNews}
export default router;